package com.canbot.u05.activity.shoppingGuide;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.adapter.ChooseAudioAdapter;
import com.canbot.u05.bean.Audio;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 导购模式选择音频
 */
public class ShoppingChooseAudioActivity extends BaseActivityNoTitle implements View.OnClickListener, AdapterView.OnItemClickListener {

        private Button btnBack;

        private TextView btnSure;

        private TextView textView_null;

        private GridView gridView;

        private List<Audio> data = new ArrayList<>();

        private Audio selectAudio;  //选择的音频

        private int lastPosition = -1;

        private ChooseAudioAdapter adapter;

        private String voice;

        private Dialog dialogLoading;

        private String audioPath;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_shopping_choose_audio);
                voice = getIntent().getStringExtra("voice");
                setViews();

                audioPath = getIntent().getStringExtra("path");
                Logger.e("音频路径=" + audioPath);
                getData();
        }

        private void setViews() {
                btnBack = (Button) findViewById(R.id.shopping_btn_delete);
                // btnCancle=(TextView)findViewById(R.id.shopping_btn_cancle);
                btnSure = (TextView) findViewById(R.id.shopping_btn_sure);
                gridView = (GridView) findViewById(R.id.shopping_gridView_audio);
                textView_null = (TextView)findViewById(R.id.textView_null);
                btnBack.setOnClickListener(this);
//        btnCancle.setOnClickListener(this);
                btnSure.setOnClickListener(this);
                gridView.setOnItemClickListener(this);
        }

        private void getData() {
                if (TextUtils.isEmpty(audioPath)) {
                        ToastTools.newInstance(this).showText(getString(R.string.audio_path_error));
                        return;
                }
                dialogLoading = LoadingDialogUtils.createLoadingDialog(ShoppingChooseAudioActivity.this, getResources().getString(R.string.request));
                dialogLoading.show();
                String url = "http://" + IpMsgConst.SOCKET_HOST + ":7755/getAudioList";
                HashMap<String, String> hashMap = new HashMap<>();
//        hashMap.put("path", "/shoppingGuide/audio");
                hashMap.put("path", audioPath);
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Log.e("t", result);
                                final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                Logger.e("save", "onSuccess:" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "strMsgBean.getRc()" + strMsgBean.getRc());
                                if (strMsgBean.getRc() == 0) {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        if (dialogLoading != null){
                                                                dialogLoading.setCancelable(true);
                                                                dialogLoading.dismiss();
                                                        }
                                                        List<Audio> list = JSON.parseArray(strMsgBean.getResult().toString(), Audio.class);
                                                        if (list == null || list.size() ==0){
                                                                Logger.e("没有音频文件");
                                                                textView_null.setVisibility(View.VISIBLE);
                                                                gridView.setVisibility(View.INVISIBLE);
                                                                return;
                                                        }
                                                        if (!TextUtils.isEmpty(voice)) {
                                                                for (int i = 0; i < list.size(); i++) {
                                                                        String path = list.get(i).getPath();
                                                                        Log.d("ShoppingChooseAudioActi", "path" + path + "---" + "voice " + voice);
                                                                        if (path.equals(voice)) {
                                                                                Logger.e("ShoppingChooseAudioActi", "path---" + path + "---" + "voice " + voice);
                                                                                list.get(i).setChecked(true);
                                                                                lastPosition = i;
                                                                        }
                                                                        else {
                                                                                list.get(i).setChecked(false);
                                                                        }
                                                                }
                                                        }
                                                        data.addAll(list);
                                                        Logger.e("ShoppingChooseAudioActi", "data=" + data.toString());
                                                        setAdapter(data);

                                                        adapter.notifyDataSetChanged();
                                                }
                                        });
                                }
                        }

                        @Override
                        public void onFailure() {
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                dialogLoading.dismiss();
                                                ToastTools.newInstance(ShoppingChooseAudioActivity.this).showText(getString(R.string.network_error));
                                        }
                                });
                        }
                });

        }

        private void setAdapter(List<Audio> data) {
                adapter = new ChooseAudioAdapter(this, data);
                gridView.setAdapter(adapter);
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.shopping_btn_delete:
                                //停止播放音频
                                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop_sound");
                                setResult(RESULT_CANCELED);
                                //返回
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;

                        case R.id.shopping_btn_sure:
                                //确定
                                MsgSendUtils.sendStringMsg(MsgType.STOP_SOUND, "stop_sound");
                                if (selectAudio == null) {
                                        ToastTools.newInstance(ShoppingChooseAudioActivity.this).showText(getString(R.string.please_select_audio));
                                        return;
                                }
                                String audioPath = selectAudio.getPath();
                                Intent intent = new Intent();
                                intent.putExtra("isChecked", true);
                                intent.putExtra("path", audioPath);
                                setResult(RESULT_OK, intent);
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                }
        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (data.get(position).isChecked()) {
                        data.get(position).setChecked(false);
                        adapter.updateView(position, gridView);
                        selectAudio = null;
                }
                else {
                        data.get(position).setChecked(true);
                        adapter.updateView(position, gridView);
                        selectAudio = data.get(position);
                }
                if (lastPosition != -1
                        && data.get(lastPosition).isChecked()
                        && lastPosition != position) {
                        data.get(lastPosition).setChecked(false);
                        adapter.updateView(lastPosition, gridView);
                }
                lastPosition = position;
                Logger.e("ShoppingChooseAudioActi", "音频预览" + data.get(position).getPath());
                String path = data.get(position).getPath();
//                String[] strings = path.split(",");
//                Log.d("ShoppingChooseAudioActi", strings[1]);
                MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, data.get(position).getPath());

        }
}
